package com.dzq.controls
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * @author Ethan.Du 
	 * @date 2008-06-20
	 * @url http://www.duzengqiang.com
	 */
[SWF(width= 200, height=200)]
public class Clock extends Sprite
{
	private var secondHand:Shape;
	private var minuteHand:Shape;
	private var hourHand:Shape;
	private var circle:Shape;
	private var date:Date;
			
	public function Clock()
	{
		stage.align = StageAlign.TOP_LEFT;
		stage.scaleMode = StageScaleMode.NO_SCALE;
		var xOff:Number = 0;
		var yOff:Number = 0;

		var g:Graphics;
		
		// Create the second hand.
		secondHand = new Shape();
		secondHand.x = 100;
		secondHand.y = 100;
		addChild(secondHand);
		
		g = secondHand.graphics;
		g.beginFill(0x000000, 0.8);
		g.moveTo(xOff, yOff);
		g.lineTo(1 + xOff, 0 + yOff);
		g.lineTo(1 + xOff, 50 + yOff);
		g.lineTo(0 + xOff - 1, 0 + yOff);
		g.lineTo(xOff, yOff);
		g.endFill();
		
		
		
		// Create the minute hand.
		minuteHand = new Shape();
		minuteHand.x = 100;
		minuteHand.y = 100;
		addChild(minuteHand);
		
		g = minuteHand.graphics;
		g.beginFill(0x000000, 0.7);
		g.moveTo(xOff, yOff);
		g.lineTo(2 + xOff, 0 + yOff);
		g.lineTo(2 + xOff, 50 + yOff);
		g.lineTo(0 + xOff - 2, 0 + yOff);
		g.lineTo(xOff, yOff);
		g.endFill();
		
		
		
		// Create the hour hand.
		hourHand = new Shape();
		hourHand.x = 100;
		hourHand.y = 100;
		addChild(hourHand);
		
		g = hourHand.graphics;
		g.beginFill(0x000000, 0.7);
		g.moveTo(xOff, yOff);
		g.lineTo(2 + xOff, 0 + yOff);
		g.lineTo(2 + xOff, 40 + yOff);
		g.lineTo(0 + xOff - 2, 0 + yOff);
		g.lineTo(xOff, yOff);
		g.endFill();
		
		
		
		circle = new Shape();
		circle.x = 100;
		circle.y = 100;
		addChild( circle );
		g = circle.graphics;
		g.beginFill( 0x000000 );
		g.drawCircle(xOff, yOff, 4)
		g.endFill();
		g.lineStyle( 2, 0x000000 ,0.6);
		drawDottedCircle(g,0, 0, 50, 5, 50*2*Math.PI/12 - 5);		
		g.endFill();
		
		
		//set timer		
		var secondTimer:Timer = new Timer(1000);
		secondTimer.addEventListener(TimerEvent.TIMER, function(e:Event):void{
					date = new Date();
					secondHand.rotation = date.seconds * 6 - 180;
					minuteHand.rotation = date.minutes * 6 - 180;
					hourHand.rotation = date.hours * 30 % 360 - 180 + date.minutes / 2;
		});
		secondTimer.start();
		
	}
	/**
		 * Draw Dotted Circle
		 * @param g the graphics context to draw with
		 * @param x the center x-coordinate of the arc
		 * @param y the center y-coorindate of the arc
		 * @param radius the radius of the arc
		 * 
		 */
		private function drawDottedCircle(g:Graphics, x:Number, y:Number, 
									radius:Number, dotLength:Number, gap:Number):void{
			for(var i:int =0; i<= radius * Math.PI * 2; i+= dotLength + gap ){
				drawArc(g, x, y, radius, i/radius , (i + dotLength)/radius );
			}							
		}
		/**
		 * Draws an arc (a segment of a circle's circumference)
		 * @param g the graphics context to draw with
		 * @param x the center x-coordinate of the arc
		 * @param y the center y-coorindate of the arc
		 * @param radius the radius of the arc
		 * @param a0 the starting angle of the arc (in radians)
		 * @param a1 the ending angle of the arc (in radians)
		 */
		public  function drawArc(g:Graphics, x:Number, y:Number, 
									radius:Number, a0:Number, a1:Number) : void
		{
			var slices:Number = (Math.abs(a1-a0) * radius) / 4;
			var a:Number, cx:Number = x, cy:Number = y;
			
			for (var i:uint = 0; i <= slices; ++i) {
				a = a0 + i*(a1-a0)/slices;
				x = cx + radius * Math.cos(a);
				y = cy + -radius * Math.sin(a);
				if (i==0) {
					g.moveTo(x, y);
				} else {
					g.lineTo(x,y);
				}
			}
		}


}
}